package com.atty.dms.controller;

import com.atty.dms.common.convention.exception.ClientException;
import com.atty.dms.common.convention.result.Result;
import com.atty.dms.common.convention.result.Results;
import com.atty.dms.dao.entity.InterestDO;
import com.atty.dms.dto.req.InterestCreateReqDTO;
import com.atty.dms.dto.req.InterestUpdateReqDTO;
import com.atty.dms.service.InterestService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName:InterestController
 * Package:com.atty.dms.controller
 * Description:
 *
 * @Author:tony
 * @Create :2025/4/17 15:21
 * @Version :1.0
 */
@RestController
@RequiredArgsConstructor
public class InterestController {

    private final InterestService interestService;

    /**
     * 根据ID获取兴趣信息
     */
    @GetMapping("/api/dms/admin/interest/{interestId}")
    public Result<InterestDO> getInterestById(@PathVariable Long interestId) {
        InterestDO interestDO = interestService.getById(interestId);
        if (interestDO == null) {
            throw new ClientException("获取新兴趣失败，请检查ID是否存在");
        }
        return Results.success(interestDO);
    }

    /**
     * 创建新兴趣
     */
    @PostMapping("/api/dms/admin/interest")
    @Transactional(rollbackFor = Exception.class)
    public Result<Void> createInterest(@RequestBody InterestCreateReqDTO requestParam) {
        InterestDO interestDO = new InterestDO();
        interestDO.setName(requestParam.getName());
        try{interestService.save(interestDO);
        }catch (Exception e){
            throw new ClientException("创建新兴趣失败，请检查名称是否重复");
        }
        return Results.success();
    }

    /**
     * 修改兴趣信息
     */
    @PutMapping("/api/dms/admin/interest")
    @Transactional(rollbackFor = Exception.class)
    public Result<Void> updateInterest(@RequestBody InterestUpdateReqDTO requestParam) {
        InterestDO interestDO = new InterestDO();
        interestDO.setInterestId(requestParam.getInterestId());
        interestDO.setName(requestParam.getName());
        boolean result = interestService.updateById(interestDO);
        if (!result){
            throw new ClientException("修改新兴趣失败，请检查ID是否存在");
        }
        return Results.success();
    }

    /**
     * 根据ID删除兴趣信息
     */
    @DeleteMapping("/api/dms/admin/interest/{interestId}")
    @Transactional(rollbackFor = Exception.class)
    public Result<Void> deleteInterestById(@PathVariable Long interestId) {
        boolean result = interestService.removeById(interestId);
        if (!result) {
            throw new ClientException("删除新兴趣失败，请检查ID是否存在");
        }
        return Results.success();
    }

    /**
     * 获取所有兴趣信息（按 ID 升序排列）
     */
    @GetMapping("/api/dms/admin/interest/all")
    public Result<List<InterestDO>> getAllInterests() {
        // 创建查询条件并指定排序规则
        List<InterestDO> interestList = interestService.list(
                new LambdaQueryWrapper<InterestDO>()
                        .orderByAsc(InterestDO::getInterestId) // 按 interestId 升序排序
        );
        if (interestList == null || interestList.isEmpty()) {
            throw new ClientException("当前没有兴趣信息");
        }
        return Results.success(interestList);
    }

    /**
     * 分页查询所有兴趣信息
     * @param pageNum
     * @param sizeNum
     * @return
     */
    @GetMapping("/api/dms/admin/interest/page")
    public Result<Page<InterestDO>> getAllInterestsByPage(
            @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize",defaultValue = "10") int sizeNum) {
        Page<InterestDO> interestPage = new Page<>(pageNum, sizeNum);
        Page<InterestDO> resultPage = interestService.page(
                interestPage,
                new LambdaQueryWrapper<InterestDO>()
                        .orderByAsc(InterestDO::getInterestId)
        );
        if (resultPage.getRecords().isEmpty()) {
            throw new ClientException("当前没有兴趣信息");
        }
        return Results.success(resultPage);
    }

    /**
     * 模糊查询兴趣信息
     * @param name
     * @return
     */
    @GetMapping("/api/dms/admin/interest/search")
    public Result<List<InterestDO>> searchInterests(@RequestParam String name) {
        List<InterestDO> interestList = interestService.list(
                new LambdaQueryWrapper<InterestDO>()
                        .like(InterestDO::getName, name) // 模糊匹配兴趣名称
                        .orderByAsc(InterestDO::getInterestId)
        );
        if (interestList == null || interestList.isEmpty()) {
            throw new ClientException("未找到符合条件的兴趣信息");
        }
        return Results.success(interestList);
    }

}
